package com.sqx.modules.recovery.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.service.GoodsService;
import com.sqx.modules.recovery.entity.GoodsRecovery;
import com.sqx.modules.recovery.service.GoodsRecoveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description：TODO
 * @author： qiao
 * @create： 2024/6/28 15:49
 */
@RestController
@RequestMapping("/admin/goodsRecovery")
@Api(value = "物品-管理端",tags = {"物品管理端"})
public class GoodsRecoveryController {

    @Autowired
    private GoodsRecoveryService goodsRecoveryService;
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/saveGoodsRecovery")
    @ApiOperation("添加物品")
    public Result saveGoodsRecovery(@ApiParam("物品实体")GoodsRecovery goodsRecovery) {
        return goodsRecoveryService.saveGoodsRecovery(goodsRecovery);
    }

    @PutMapping("/updateGoodsRecovery")
    @ApiOperation("修改物品")
    public Result updateGoodsRecovery(@ApiParam("物品实体")GoodsRecovery goodsRecovery) {
        return goodsRecoveryService.updateGoodsRecovery(goodsRecovery);
    }

    @PostMapping("/deleteGoodsRecovery")
    @ApiOperation("删除物品")
    public Result deleteGoodsRecovery(@ApiParam("物品id")Long id) {
        List<Goods> goodsList = goodsService.list(new QueryWrapper<Goods>().eq("recovery_id",id));
        if (!goodsList.isEmpty()) {
            return Result.error("该物品下有关联，请勿删除");
        }
        return goodsRecoveryService.deleteGoodsRecovery(id);
    }

    @GetMapping("/getGoodsRecoveryList")
    @ApiOperation("获取物品列表")
    public Result getGoodsRecoveryList(@ApiParam("页码")@RequestParam(value = "pageNum") Integer pageNum,
                                       @ApiParam("条数")@RequestParam(value = "pageSize") Integer pageSize,
                                       @ApiParam("物品名称")@RequestParam(value = "name", required = false) String name,
                                       @ApiParam("品牌id")@RequestParam(value = "brandId",required = false)Long brandId) {
        return goodsRecoveryService.getGoodsRecoveryList(pageNum, pageSize,name,brandId);
    }

    @GetMapping("/getGoodsRecoveryById")
    @ApiOperation("获取物品详情")
    public Result getGoodsRecoveryById(@ApiParam("物品id")@RequestParam(value = "id") Long id) {
        return goodsRecoveryService.getGoodsRecoveryById(id);
    }
}
